package org.szgb.console.zx.task.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
import org.szgb.console.sys.file.entity.SysFile;
import org.szgb.console.sys.line.entity.SysLineLocation;
import org.szgb.console.sys.user.entity.SysUser;
import org.szgb.console.utils.PageDto;
import org.szgb.console.zx.plan.entity.ZxPlan;
import org.szgb.core.base.entity.BeanEntity;

import java.io.Serial;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class ZxTask extends BeanEntity<ZxTask> {
    @Serial
    private static final long serialVersionUID = 1L;
    private ZxPlan zxPlan;
    private Integer status;
    private SysLineLocation location;
    private SysUser leader;
    private String memberIds;
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate date;

    public ZxTask() {
    }

    public ZxTask(String id) {
        this.setId(id);
    }

    public ZxTask(ZxTask.ZxTaskFindDto zxTaskFindDto) {
        this.setId(zxTaskFindDto.getId());
        this.setZxPlan(new ZxPlan(zxTaskFindDto.getZxPlanId()));
        this.setStatus(zxTaskFindDto.getStatus());
        this.setLocation(new SysLineLocation(zxTaskFindDto.getLocationId()));
        this.setLeader(new SysUser(zxTaskFindDto.getLeaderId()));
        this.setDate(zxTaskFindDto.getDate());
        this.setPageDto(zxTaskFindDto.getPageDto());
    }

    public ZxTask(ZxTask.ZxTaskSaveDto zxTaskSaveDto) {
        this.setId(zxTaskSaveDto.getId());
        this.setZxPlan(new ZxPlan(zxTaskSaveDto.getZxPlanId()));
        this.setStatus(zxTaskSaveDto.getStatus());
        this.setLocation(new SysLineLocation(zxTaskSaveDto.getLocationId()));
        this.setLeader(new SysUser(zxTaskSaveDto.getLeaderId()));
        this.setMemberIds(zxTaskSaveDto.getMemberIds());
        this.setDate(zxTaskSaveDto.getDate());
    }

    public ZxTask(ZxTask.ZxTaskAssignDto zxTaskAssignDto) {
        this.setId(zxTaskAssignDto.getId());
        this.setLeader(new SysUser(zxTaskAssignDto.getLeaderId()));
        this.setMemberIds(zxTaskAssignDto.getMemberIds());
        this.setDate(zxTaskAssignDto.getDate());
    }

    public ZxTask.ZxTaskView toZxTaskView(String memberNames) {
        return new ZxTask.ZxTaskView(this, memberNames);
    }

    public ZxTask.ZxTaskListView toZxTaskListView() {
        return new ZxTask.ZxTaskListView(this);
    }

    @Data
    @Schema(title = "专项任务信息", description = "专项任务信息")
    public static class ZxTaskView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "计划id")
        private String zxPlanId;
        @Schema(description = "计划名称")
        private String zxPlanName;
        @Schema(description = "牵头人id")
        private String zxPlanLeaderId;
        @Schema(description = "状态")
        private Integer status;
        @Schema(description = "站点id")
        private String locationId;
        @Schema(description = "站点名称")
        private String locationName;
        @Schema(description = "负责人id")
        private String leaderId;
        @Schema(description = "负责人名称")
        private String leaderName;
        @Schema(description = "成员ids")
        private String memberIds;
        @Schema(description = "站点成员名称")
        private String memberNames;
        @Schema(description = "专项描述")
        private String zxPlanRemark;
        @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        @Schema(description = "日期")
        private LocalDate date;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人id")
        private String createdById;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人id")
        private String updatedById;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public ZxTaskView(ZxTask zxTask, String memberNames) {
            this.id = zxTask.getId();
            if (zxTask.getZxPlan() != null) {
                this.zxPlanId = zxTask.getZxPlan().getId();
                this.zxPlanName = zxTask.getZxPlan().getName();
                this.zxPlanRemark = zxTask.getZxPlan().getRemark();
                if (zxTask.getZxPlan().getLeader() != null) {
                    this.zxPlanLeaderId = zxTask.getZxPlan().getLeader().getId();
                }
            }
            this.status = zxTask.getStatus();
            if (zxTask.getLocation() != null) {
                this.locationId = zxTask.getLocation().getId();
                this.locationName = zxTask.getLocation().getName();
            }
            if (zxTask.getLeader() != null) {
                this.leaderId = zxTask.getLeader().getId();
                this.leaderName = zxTask.getLeader().getName();
            }
            this.memberIds = zxTask.getMemberIds();
            this.memberNames = memberNames;
            this.date = zxTask.getDate();
            this.createdAt = zxTask.getCreatedAt();
            this.updatedAt = zxTask.getUpdatedAt();
            if (zxTask.getCreatedBy() != null) {
                this.createdById = zxTask.getCreatedBy().getId();
                this.createdByName = zxTask.getCreatedBy().getName();
            }
            if (zxTask.getUpdatedBy() != null) {
                this.updatedById = zxTask.getUpdatedBy().getId();
                this.updatedByName = zxTask.getUpdatedBy().getName();
            }
        }
    }

    @Data
    @Schema(title = "专项任务列表信息", description = "专项任务列表信息")
    public static class ZxTaskListView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "计划名称")
        private String zxPlanName;
        @Schema(description = "状态")
        private Integer status;
        @Schema(description = "站点名称")
        private String locationName;
        @Schema(description = "牵头人名称")
        private String leaderName;
        @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        @Schema(description = "日期")
        private LocalDate date;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public ZxTaskListView(ZxTask zxTask) {
            this.id = zxTask.getId();
            this.zxPlanName = zxTask.getZxPlan().getName();
            this.status = zxTask.getStatus();
            this.locationName = zxTask.getLocation().getName();
            if (zxTask.getLeader() != null) {
                this.leaderName = zxTask.getLeader().getName();
            }
            this.date = zxTask.getDate();
            this.createdAt = zxTask.getCreatedAt();
            this.updatedAt = zxTask.getUpdatedAt();
            if (zxTask.getCreatedBy() != null) {
                this.createdByName = zxTask.getCreatedBy().getName();
            }
            if (zxTask.getUpdatedBy() != null) {
                this.updatedByName = zxTask.getUpdatedBy().getName();
            }
        }

        public static List<ZxTask.ZxTaskListView> getListViewListFromList(List<ZxTask> zxTaskList) {
            return zxTaskList.stream().map(ZxTask.ZxTaskListView::new).toList();
        }
    }

    @Data
    @Schema(title = "专项任务表单信息", description = "专项任务表单")
    public static class ZxTaskTableView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "计划名称")
        private String zxPlanName;
        @Schema(description = "站点名称")
        private String locationName;
        @Schema(description = "牵头人名称")
        private String leaderName;
        @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        @Schema(description = "日期")
        private LocalDate date;
        @Schema(description = "数据要求")
        private String requirement;
        @Schema(description = "专项描述")
        private String zxPlanRemark;
        @Schema(description = "任务文件列表")
        private List<SysFile.SysFileView> taskFileList;
        @Schema(description = "表单子项列表")
        private List<ZxTaskForm.ZxTaskFormItemView> zxTaskFormItemViewList;

        public ZxTaskTableView(ZxTask zxTask, List<ZxTaskForm.ZxTaskFormItemView> zxTaskFormItemViewList, List<SysFile.SysFileView> taskFileList) {
            this.id = zxTask.getId();
            this.zxPlanName = zxTask.getZxPlan().getName();
            this.locationName = zxTask.getLocation().getName();
            if (zxTask.getLeader() != null) {
                this.leaderName = zxTask.getLeader().getName();
            }
            this.date = zxTask.getDate();
            this.requirement = zxTask.getZxPlan().getRequirement();
            this.zxPlanRemark = zxTask.getZxPlan().getRemark();
            this.taskFileList = taskFileList;
            this.zxTaskFormItemViewList = zxTaskFormItemViewList;
        }
    }

    @Data
    @Schema(title = "专项任务查询表单", description = "专项任务查询参数")
    public static class ZxTaskFindDto {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "计划id")
        private String zxPlanId;
        @Schema(description = "状态")
        private Integer status;
        @Schema(description = "站点id")
        private String locationId;
        @Schema(description = "负责人id")
        private String leaderId;
        @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        @Schema(description = "日期")
        private LocalDate date;
        @Schema(description = "分页信息")
        private PageDto pageDto;

        public ZxTask toZxTask() {
            return new ZxTask(this);
        }
    }

    @Data
    @Schema(title = "专项任务表单牵头人退回表单", description = "专项任务表单牵头人退回表单")
    public static class ZxTaskLeaderReturnFormDto {
        @Schema(description = "任务id")
        private String id;
        @Schema(description = "备注")
        private String remark;

        public ZxTask toZxTask() {
            return new ZxTask(this.getId());
        }
    }

    @Data
    @Schema(title = "专项任务保存表单", description = "专项任务保存参数")
    public static class ZxTaskSaveDto {
        @Schema(description = "id(如为空则为新增数据)")
        private String id;
        @Schema(description = "计划id")
        private String zxPlanId;
        @Schema(description = "状态")
        private Integer status;
        @Schema(description = "站点id")
        private String locationId;
        @Schema(description = "负责人id")
        private String leaderId;
        @Schema(description = "成员ids")
        private String memberIds;
        @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        @Schema(description = "日期")
        private LocalDate date;

        public ZxTask toZxTask() {
            return new ZxTask(this);
        }
    }

    @Data
    @Schema(title = "专项任务下发表单", description = "专项任务下发参数")
    public static class ZxTaskAssignDto {
        @Schema(description = "id")
        private String id;
        @Schema(description = "负责人id")
        private String leaderId;
        @Schema(description = "成员ids")
        private String memberIds;
        @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        @Schema(description = "日期")
        private LocalDate date;

        public ZxTask toZxTask() {
            return new ZxTask(this);
        }
    }

    @Data
    @Schema(title = "专项任务表单提交表单", description = "专项任务表单提交参数")
    public static class ZxTaskSubmitDto {
        @Schema(description = "id(如为空则为新增数据)")
        private String id;
        @Schema(description = "专项任务表单")
        private List<ZxTaskForm.ZxTaskFormSubmitDto> zxTaskFormSubmitDtoList;

        public ZxTask toZxTask() {
            return new ZxTask(this.getId());
        }
    }
}
